package util;

import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.sql.Connection;


/**
 * 工具类
 * @author Alex
 *
 */
public class MybatisUtil {
	private static ThreadLocal<SqlSession>threadLocal=new ThreadLocal<SqlSession>();
	private static SqlSessionFactory sqlSessionFactory;
	/**
	 * 加载位于src/mybatis.xml配置文件
	 */
	static{
		try{
			Reader reader=Resources.getResourceAsReader("mybatis.xml");
			sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
		}catch(IOException e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	
	}
	 /**
	  * 禁止外界通过new方法创建
	  */
	private MybatisUtil(){}
	/**
	 * 获取SqlSession
	 */
    public static SqlSession getSqlSession() {
    	//从当前线程中获取SqlSession对象
    	SqlSession sqlSession=threadLocal.get();
    	//如果SqlSession对象为空
    	if (sqlSession==null) {
    		//在SqlSessionFactory非空的情况下，获取SqlSessio对象
			sqlSession=sqlSessionFactory.openSession();
			//将SqlSession对象和当前线程绑在一起
			threadLocal.set(sqlSession);
		}
		return sqlSession;
	}
    /**
	 * 关闭SqlSession与当前线程分开
	 */
    public static void closeSqlSession() {
    	//从当前线程中获取SqlSession对象
    	SqlSession sqlSession=threadLocal.get();
    	//如果SqlSession对象非空
    	if (sqlSession!=null){
    		//关闭SqlSession对象
    		sqlSession.close();
    		//分开当前线程和SqlSession对象的关系，目的是让GC尽早回收
    		threadLocal.remove();
    	}
		
	}
    
    
    /**
     * 测试
     */
    
    public static void main(String[] args) {
    	Connection conn = MybatisUtil.getSqlSession().getConnection();
		System.out.println(conn!=null?"连接成功":"连接失败");
	}
}
